package io.renren.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

import io.renren.entity.DictWagesTypeEntity;
import io.renren.entity.SysUserEntity;
import io.renren.entity.xtd.DictWagesTypeEntityXtd;
import io.renren.service.DictWagesTypeService;
import io.renren.service.xtd.DictWagesTypeServiceXtd;
import io.renren.utils.PageUtils;
import io.renren.utils.R;
import io.renren.utils.ShiroUtils;


/**
 * 薪资类型表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-05-21 10:31:22
 */
@Controller
@RequestMapping("dictwagestype")
public class DictWagesTypeController {
	@Autowired
	private DictWagesTypeService dictWagesTypeService;
	
	@Autowired
	private DictWagesTypeServiceXtd dictWagesTypeServiceXtd;
	
	@RequestMapping("/dictwagestype.html")
	public String list(){
		return "dictwagestype/dictwagestype.html";
	}
	
	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("dictwagestype:list")
	public R list(Integer page, Integer limit){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		
		//查询列表数据
		List<DictWagesTypeEntity> dictWagesTypeList = dictWagesTypeService.queryList(map);
		int total = dictWagesTypeService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(dictWagesTypeList, total, limit, page);
		
		return R.ok().put("page", pageUtil);
	}
	
	@ResponseBody
	@RequestMapping("/listXtd")
	@RequiresPermissions("dictwagestype:list")
	public R listXtd(Integer page, Integer limit){
		Map<String, Object> map = new HashMap<>();
		map.put("offset", (page - 1) * limit);
		map.put("limit", limit);
		
		//查询列表数据
		List<DictWagesTypeEntityXtd> dictWagesTypeListXtd = dictWagesTypeServiceXtd.queryListXtd(map);
		int total = dictWagesTypeService.queryTotal(map);
		
		PageUtils pageUtil = new PageUtils(dictWagesTypeListXtd, total, limit, page);
		
		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 信息
	 */
	@ResponseBody
	@RequestMapping("/info/{id}")
	@RequiresPermissions("dictwagestype:info")
	public R info(@PathVariable("id") String id){
		DictWagesTypeEntity dictWagesType = dictWagesTypeService.queryObject(id);
		
		return R.ok().put("dictWagesType", dictWagesType);
	}
	
	@ResponseBody
	@RequestMapping("/infoXtd/{id}")
	@RequiresPermissions("dictwagestype:info")
	public R infoXtd(@PathVariable("id") String id){
		DictWagesTypeEntityXtd dictWagesTypeXtd = dictWagesTypeServiceXtd.queryObjectXtd(id);
		
		return R.ok().put("dictWagesType", dictWagesTypeXtd);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@RequestMapping("/save")
	@RequiresPermissions("dictwagestype:save")
	public R save(@RequestBody DictWagesTypeEntity dictWagesType){
		dictWagesTypeService.save(dictWagesType);
		
		return R.ok();
	}
	
	@ResponseBody
	@RequestMapping("/saveXtd")
	@RequiresPermissions("dictwagestype:save")
	public R saveXtd(@RequestBody DictWagesTypeEntityXtd dictWagesType){
		SysUserEntity user =ShiroUtils.getUserEntity();
		if(user==null || StringUtils.isBlank(user.getUsername())){
			return R.error("账号已失效，请重新登录");
		}
		Date nowData=new Date();
		dictWagesType.setCreateby(user.getUsername());
		dictWagesType.setCreatetime(nowData);
		dictWagesTypeServiceXtd.saveXtd(dictWagesType);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("dictwagestype:update")
	public R update(@RequestBody DictWagesTypeEntity dictWagesType){
		dictWagesTypeService.update(dictWagesType);
		
		return R.ok();
	}
	
	@ResponseBody
	@RequestMapping("/updateXtd")
	@RequiresPermissions("dictwagestype:update")
	public R updateXtd(@RequestBody DictWagesTypeEntityXtd dictWagesType){
		SysUserEntity user =ShiroUtils.getUserEntity();
		if(user==null || StringUtils.isBlank(user.getUsername())){
			return R.error("账号已失效，请重新登录");
		}
		Date nowData=new Date();
		dictWagesType.setUpdateby(user.getUsername());
		dictWagesType.setUpdatetime(nowData);
		dictWagesTypeService.update(dictWagesType);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("dictwagestype:delete")
	public R delete(@RequestBody String[] ids){
		dictWagesTypeService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
